/*
 * @Description:
 * @Author: Zzt
 * @Date: 2022-10-26 20:01:56
 * @LastEditTime: 2022-10-31 11:53:45
 */
import {
  ADD_BOXLIST,
  DELETE_BOXLIST,
  EDIT_BOXLIST,
  SET_SELECT_KEY,
  GET_BOXLIST
} from '../constants';
import { delItemByIdArray, editItemById, insertItem } from '@/utils/list';

export const BoxConfigReducer = (state, action) => {
  switch (action.type) {
    case GET_BOXLIST: {
      return {
        ...state,
        boxListData: action.data
      };
    }
    case SET_SELECT_KEY: {
      return {
        ...state,
        selectItems: action.data
      };
    }
    /**
     * @description: 添加规则配置
     */
    case ADD_BOXLIST: {
      return {
        ...state,
        boxListData: insertItem(action.data, state.boxListData)
      };
    }
    /**
     * @description: 删除规则配置
     */
    case DELETE_BOXLIST: {
      return {
        ...state,
        boxListData: delItemByIdArray(action.ids, state.boxListData)
      };
    }
    /**
     * @description: 编辑规则配置
     */
    case EDIT_BOXLIST: {
      const { id, data } = action;
      console.log(action);
      return {
        ...state,
        boxListData: editItemById(id, data, state.boxListData)
      };
    }
    default:
      console.log('untouched', action);
      return state;
  }
};
